// pages/square/square.js
var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        shoppingList:[],
        recommandList:[],
        addressList:[],
        selectshoppingList:[],
        sumPrice:0.00,
        allSelectState: false
    },
    ///修改默认地址(点击事件)
    boult:function(){
      wx.navigateTo({
        url: '/pages/mine/mine',
      })
    },
    ///点击金额结算 有bug
    amount(){
      let cart = wx.getStorageSync('cart')||[];
      console.log(cart)
      wx.navigateTo({
        url: '/pages/amount/amount',
      })
    },
    ///获取默认地址信息
    getAddressList(){
      wx.get('http://localhost:8000/wx/shopping/address').then(res => {
        // console.log(res)
        this.setData({
          addressList:res
        })
      })
    },
    ///获取购物车信息
    getShoppingList(){
       //获取购物车数据
       let cart = wx.getStorageSync('cart')||[];
       this.setData({
         shoppingList:cart
        });
        // console.log("购物车数据：")
        // console.log(cart)
      },
      ///获取为你推荐信息
      getRecommandList(){
        wx.get('http://localhost:8000/wx/recommand').then(res => {
        //   console.log(res)
          this.setData({
            recommandList:res
          })
        })
      },
      recommand(e){
        // console.log(e)
        // let id = e.currentTarget.dataset.id
        wx.navigateTo({
          url: '/pages/recommand/recommand?id=' + e.currentTarget.dataset.id,
        })
      },
      radiocon:function(e){
        var check = this.data.check;
          check =! check;
          this.setData({
            check:check
          })
      },

      ///选择商品(单选)
      selectGood(e){
        //获取购物车列表
        let cart = wx.getStorageSync('cart') || [];
        //获取购物车全选状态
        let allSelect = this.data.allSelectState;
        // 获取当前选择商品
        let good = e.currentTarget.dataset.good;
        let index = cart.findIndex(v=>v.id===good.id);
        ///isChecked默认为false
        if(good.isChecked){
          console.log('取消勾选商品');
          this.decreaseSumPrice(Number(good.price),parseInt(good.num))
          cart[index].isChecked = false;
          allSelect = false;
        }else{
          console.log('勾选商品');
          this.addSumPrice(Number(good.price),Number(good.num))
          cart[index].isChecked = true;
        }
         this.setData({
           shoppingList:cart,
           allSelectState:allSelect
         })
         wx.setStorageSync('cart', cart)
        // console.log(this.data.shoppingList)
      },

      ///全选
      allSelectgood(e){
        let cart = wx.getStorageSync('cart')||[];
        let allState = this.data.allSelectState;
        this.setData({
          sumPrice:0
        });
        // console.log(cart)
        //汇总商品金额,修改商品选择状态
        //取消全选
        // console.log(allState)
        // console.log(typeof allState)
        if(allState){
          // console.log('取消全选');
          allState = false;
          for(var index in cart){
            cart[index].isChecked = false;
          }
        //全选
        }else{
          // console.log('全选');
          allState = true;
          for(var index in cart){
            this.addSumPrice(Number(cart[index].price),cart[index].num)
            cart[index].isChecked = true;
          }
        }
        wx.setStorageSync('cart', cart)
        this.setData({
          shoppingList:cart,
          allSelectState:allState
        })
      },

      ///添加合计总金额
      addSumPrice(price,num){
        this.setData({
          sumPrice:Number((this.data.sumPrice+price*num).toFixed(2))
        })
      },

      ///减少合计总金额
      decreaseSumPrice(price,num){
        this.setData({
          sumPrice:Number((this.data.sumPrice-price*num).toFixed(2))
        })
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getShoppingList();
        this.getRecommandList();
        this.getAddressList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {   //页面显示时出现2秒的加载过程
      wx.showToast({
        title: 'loading',
        icon:'loading',
        duration:500
      });
      this.getShoppingList();
      this.getRecommandList();
      this.getAddressList();
    },
    /**
     * 删除购物车
     */
    deleteGood(e){
      let cart = wx.getStorageSync('cart')||[]
      let index = cart.findIndex(v=>v.id===e.currentTarget.dataset.good.id);
      //删除购物车商品
      if(index!==-1){
        ///已经存在 删除
        cart.splice(index,1)
    }
    //重新放入storage缓存中
    wx.setStorageSync('cart', cart)
    this.setData({
      shoppingList:cart
    })
    },

    ///减少商品数量
    decreaseNum(e){
      let cart = wx.getStorageSync('cart')||[];
      let index = cart.findIndex(g=>g.id===e.currentTarget.dataset.good.id)
      if(index!==-1){
        if(cart[index].num > 1){
          //修改商品总价格
          if(cart[index].isChecked){
            this.decreaseSumPrice(Number(cart[index].price),1)
          }
          cart[index].num--;
        }else{
          wx.showToast({
            title: '该宝贝不能在减少了哦~',
            icon:'none'
          })
        }
        
        wx.setStorageSync('cart', cart);
        this.setData({
          shoppingList : cart
        })
      }
    },

    ///增加商品数量
    addNum(e){
      let cart = wx.getStorageSync('cart')||[];
      let index = cart.findIndex(g=>g.id===e.currentTarget.dataset.good.id)
      if(index!==-1){
        //修改商品总价格
        if(cart[index].isChecked){
          this.addSumPrice(Number(cart[index].price),1)
        }
        cart[index].num++;
        }
        wx.setStorageSync('cart', cart);
        this.setData({
          shoppingList : cart
        })
      },

      ///清理商品选择状态
      changeIsCheckedFalse(){
        let cart = wx.getStorageSync('cart')||[];
        for(let i = 0;i<cart.length;i++){
          cart[i].isChecked = false;
        }
        wx.setStorageSync('cart', cart);
      },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
      //清理购物查选择状态
      let cart = wx.getStorageSync('cart') || [];
      for(let i = 0;i<cart.length;i++){
        cart[i].isChecked = false;
      }
      console.log(cart)
      wx.setStorageSync('cart', cart)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      this.changeIsCheckedFalse();
    },  

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
    
})